<?php

if (is_array($_POST) && !empty($_POST)) {
	if ($_POST['token'] == 'dfglkdffp934509258dflkaj') {
		require_once "Mail.php";
		$from = "amitavroy@gmail.com";
		$to = $_POST['to'];
		$subject = $_POST['subject'];
		$body = $_POST['body'];

		$host = "ssl://smtp.gmail.com";
		$port = "465";
		$username = "amitavroy@gmail.com";  //<> give errors
		$password = "brisinger";

		$headers = array ('From' => $from,
			'To' => $to,
			'Subject' => $subject,
			'From' => 'Amitav Roy <amitavroy@gmail.com>',
			'Content-type' => 'text/html; charset=iso-8859-1');
		$smtp = Mail::factory('smtp',
			array ('host' => $host,
				'port' => $port,
				'auth' => true,
				'username' => $username,
				'password' => $password));

		$mail = $smtp->send($to, $headers, $body);

		if (PEAR::isError($mail)) {
			echo("<p>" . $mail->getMessage() . "</p>");
		}
		else {
			echo json_encode('Mail send successfully to ' . $to);
		}
	}
}
else {
	echo 'Go die somewhere else.';
	die();
}